package com.fqgj.xjd.user.service.impl;
/**
 * Created by zhangxingyun on 17/7/23.
 */

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.xjd.user.client.UserAuthPictureService;
import com.fqgj.xjd.user.common.enums.UserErrorCodeEnum;
import com.fqgj.xjd.user.dao.UserAuthFaceRecognitionDao;
import com.fqgj.xjd.user.dao.UserAuthPictureDao;
import com.fqgj.xjd.user.client.response.UserAuthFaceRecognition;
import com.fqgj.xjd.user.client.response.UserAuthFaceRecognitionHistory;
import com.fqgj.xjd.user.entity.UserAuthFaceRecognitionEntity;
import com.fqgj.xjd.user.client.enums.FaceFailReasonTypeEnum;
import com.fqgj.xjd.user.client.enums.FaceRecognitionTypeEnum;
import com.fqgj.xjd.user.service.FaceRecognitionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName:
 * @Description: 用户图片认证服务
 * @Author zhangxingyun
 * @Create 2017-07-23 16:27
 */
@Service("userAuthPictureService")
public class UserAuthPictureServiceImpl implements UserAuthPictureService {

    @Autowired
    UserAuthPictureDao userAuthPictureDao;

    @Autowired
    UserAuthFaceRecognitionDao userAuthFaceRecognitionDao;

    @Autowired
    FaceRecognitionService faceRecognitionService;

    /**
     * 保存人脸识别信息
     * @param userAuthFaceRecognition
     * @param faceRecognitionTypeEnum
     * @return
     */
    @Override
    @Transactional
    public Response<Boolean> addFaceRecognitionAuth(UserAuthFaceRecognition userAuthFaceRecognition, FaceRecognitionTypeEnum faceRecognitionTypeEnum) {
        UserAuthFaceRecognitionEntity userAuthFaceRecognitionEntity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userAuthFaceRecognition.getUserCode(),faceRecognitionTypeEnum.getType());
        if (userAuthFaceRecognitionEntity != null) {
            userAuthFaceRecognitionEntity.setDeleted(true);
            userAuthFaceRecognitionEntity.setDeleteTime(System.currentTimeMillis() / 1000);
            userAuthFaceRecognitionDao.updateByPrimaryKey(userAuthFaceRecognitionEntity);
        }

        userAuthFaceRecognitionEntity = new UserAuthFaceRecognitionEntity();
        BeanUtils.copyProperties(userAuthFaceRecognition,userAuthFaceRecognitionEntity);
        String bizNo = org.apache.commons.lang3.StringUtils.isBlank(userAuthFaceRecognition.getBizNo()) ? userAuthFaceRecognition.getUserCode() : userAuthFaceRecognition.getBizNo();
        userAuthFaceRecognitionEntity.setType(faceRecognitionTypeEnum.getType());
        userAuthFaceRecognition.setBizNo(bizNo);
        userAuthFaceRecognitionDao.insert(userAuthFaceRecognitionEntity);

        return Response.ok().putData(true);
    }

    /**
     * 人脸识别成功
     * @param userAuthFaceRecognition
     * @param faceRecognitionTypeEnum
     * @return
     */
    @Override
    public Response<Boolean> faceRecognitionAuthSuccess(UserAuthFaceRecognition userAuthFaceRecognition, FaceRecognitionTypeEnum faceRecognitionTypeEnum) {
        faceRecognitionService.add(userAuthFaceRecognition,faceRecognitionTypeEnum);
        return Response.ok().putData(true);
    }

    /**
     * 人脸识别失败
     * @param userAuthFaceRecognitionHistory
     * @return
     */
    @Override
    public Response<Boolean> faceRecognitionAuthFailed(UserAuthFaceRecognitionHistory userAuthFaceRecognitionHistory) {
        faceRecognitionService.addFiledHistory(userAuthFaceRecognitionHistory);
        return Response.ok();
    }

    /**
     * 更新人脸信息
     * @param userAuthFaceRecognition
     * @param faceRecognitionTypeEnum
     * @return
     */
    @Override
    public Response<Boolean> updateFaceRecognition(UserAuthFaceRecognition userAuthFaceRecognition, FaceRecognitionTypeEnum faceRecognitionTypeEnum) {

        UserAuthFaceRecognitionEntity userAuthFaceRecognitionEntity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userAuthFaceRecognition.getUserCode(),faceRecognitionTypeEnum.getType());
        if (userAuthFaceRecognitionEntity == null) {
            return Response.error();
        }
        if (StringUtils.isNotEmpty(userAuthFaceRecognition.getBizNo())) {
            userAuthFaceRecognitionEntity.setBizNo(userAuthFaceRecognition.getBizNo());
        }
        if (userAuthFaceRecognition.getPassed() != null) {
            userAuthFaceRecognitionEntity.setPassed(userAuthFaceRecognition.getPassed());
        }
        if (StringUtils.isNotEmpty(userAuthFaceRecognition.getFrontUrl())) {
            userAuthFaceRecognitionEntity.setFrontUrl(userAuthFaceRecognition.getFrontUrl());
        }
        if (StringUtils.isNotEmpty(userAuthFaceRecognition.getBackUrl())) {
            userAuthFaceRecognitionEntity.setBackUrl(userAuthFaceRecognition.getBackUrl());
        }
        if (userAuthFaceRecognition.getDetailId() != null) {
            userAuthFaceRecognitionEntity.setDetailId(userAuthFaceRecognition.getDetailId());
        }

        userAuthFaceRecognitionDao.updateByPrimaryKey(userAuthFaceRecognitionEntity);

        return Response.ok().putData(true);
    }


    /**
     * 获取用户人脸图片信息
     * @param userCode
     * @return
     */
    @Override
    public Response<UserAuthFaceRecognition> getFaceRecognitionByUserCodeAndType(String userCode,FaceRecognitionTypeEnum faceRecognitionTypeEnum) {

        UserAuthFaceRecognitionEntity userAuthFaceRecognitionEntity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userCode,faceRecognitionTypeEnum.getType());
        if (userAuthFaceRecognitionEntity == null) {
            return Response.ok().putData(null);
        }

        UserAuthFaceRecognition userAuthFaceRecognition = new UserAuthFaceRecognition();

        BeanUtils.copyProperties(userAuthFaceRecognitionEntity,userAuthFaceRecognition);

        return Response.ok().putData(userAuthFaceRecognition);
    }

    /**
     * 获取用户人脸图片信息
     * @param userCode
     * @return
     */
    @Override
    public Response<List<UserAuthFaceRecognition>> getFaceRecognitionByUserCode(String userCode) {

        List<UserAuthFaceRecognition> userAuthFaceRecognitionList = faceRecognitionService.getUserAuthFaceRecognitionByUserCode(userCode);

        return Response.ok().putData(userAuthFaceRecognitionList);
    }

    /**
     * 通过userId与类型获取用户人脸识别失败次数
     * @param userCode
     * @param faceFailReasonTypeEnumList
     * @return
     */
    @Override
    public Response<List<UserAuthFaceRecognitionHistory>> getFaceRecognitionHistoryListByUserCodeAndTypes(String userCode, List<FaceFailReasonTypeEnum> faceFailReasonTypeEnumList) {

        List<Integer> reasonList = new ArrayList<Integer>();

        for(FaceFailReasonTypeEnum faceFailReasonTypeEnum :faceFailReasonTypeEnumList){
            reasonList.add(faceFailReasonTypeEnum.getType());
        }
        List<UserAuthFaceRecognitionHistory> userAuthFaceRecognitionHistoryList = faceRecognitionService.getUserAuthFaceRecognitionHistoryByUserCodeAndType(userCode,reasonList);
        return Response.ok().putData(userAuthFaceRecognitionHistoryList);
    }

    /**
     * 删除人脸识别历史信息
     * @param ids
     * @param faceFailReasonTypeEnumList
     * @return
     */
    @Override
    public Response<Boolean> deleteFaceRecognitionHistoriesByIdsAndTypes(List<Long> ids, List<FaceFailReasonTypeEnum> faceFailReasonTypeEnumList) {
        List<Integer> reasonList = new ArrayList<Integer>();

        for(FaceFailReasonTypeEnum faceFailReasonTypeEnum :faceFailReasonTypeEnumList){
            reasonList.add(faceFailReasonTypeEnum.getType());
        }
        faceRecognitionService.deleteFaceRecognitionHistoriesByIdsAndTypes(ids,reasonList);
        return Response.ok();
    }

    @Override
    public Response<Boolean> auditFaceRecognitionPass(String userCode, FaceRecognitionTypeEnum faceRecognitionTypeEnum) {
        UserAuthFaceRecognitionEntity entity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userCode, faceRecognitionTypeEnum.getType());
        if (entity == null) {
            return Response.error(UserErrorCodeEnum.PICTURE_NOT_EXIST);
        }
        entity.setPassed(1);
        userAuthFaceRecognitionDao.updateByPrimaryKey(entity);
        return Response.ok();
    }

    @Override
    public Response<Boolean> auditFaceRecognitionReject(String userCode, FaceRecognitionTypeEnum faceRecognitionTypeEnum, FaceFailReasonTypeEnum reason) {
        UserAuthFaceRecognitionEntity entity = userAuthFaceRecognitionDao.selectUserAuthFaceRecognitionByUserCodeAndType(userCode, faceRecognitionTypeEnum.getType());
        if (entity == null) {
            return Response.error(UserErrorCodeEnum.PICTURE_NOT_EXIST);
        }
        entity.setDeleted(true);
        entity.setDeleteTime(System.currentTimeMillis() / 1000);
        userAuthFaceRecognitionDao.updateByPrimaryKey(entity);
        UserAuthFaceRecognitionHistory history = new UserAuthFaceRecognitionHistory();
        history.setUserCode(userCode)
                .setReasonType(reason.getType())
                .setReason(reason.getDesc())
                .setFrontDetail(entity.getFrontUrl())
                .setBackDetail(entity.getBackUrl())
                .setBackDetail(entity.getFaceBestUrl());
        faceRecognitionAuthFailed(history);
        return null;
    }
}
